package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.trkj.dao.jpa.IHWCTbdepartmentDAO;
import com.trkj.dao.jpa.IYzrStorehouseDAO;
import com.trkj.dao.jpa.LmbConsumableCollarDAO;
import com.trkj.dao.jpa.LmbConsumableCollarDetailDAO;
import com.trkj.dao.jpa.LmbConsumptiveStockDAO;
import com.trkj.dao.jpa.LmbTbstaffDAO;
import com.trkj.pojos.LmbConsumableCollar;
import com.trkj.pojos.LmbConsumableCollarDetail;
import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrStorehouse;
import com.trkj.vo.LmbCkVo;
import com.trkj.vo.LmbCouYlVo;
import com.trkj.vo.LmbHlVo;
import com.trkj.vo.LmbKcVo;

@Service
@Transactional
public class LmbCouPurYlService {
	@Autowired
	private LmbConsumableCollarDAO consumableCollarDAO;
	@Autowired
	private IHWCTbdepartmentDAO tbdepartmentDAO;
	@Autowired
	private LmbTbstaffDAO tbstaffDAO;
	@Autowired
	private IYzrStorehouseDAO storehouseDAO;
	@Autowired
	private LmbConsumableCollarDetailDAO conCollarDetailDAO;
	@Autowired
	private LmbConsumptiveStockDAO conStockDAO;
	@Autowired
	private LmbOddNumbersService oddNumbersService;
	
	public LmbCouYlVo hlPoToVo(LmbConsumableCollar consumableCollar){
		LmbCouYlVo couLyVo= new LmbCouYlVo();
		couLyVo.setCollarId(consumableCollar.getCollarId());
		couLyVo.setCollarDate(consumableCollar.getCollarDate());
		couLyVo.setCollarState(consumableCollar.getCollarState());
		couLyVo.setCollarApprovedate(consumableCollar.getCollarApprovedate());
		couLyVo.setCollarBeizhu(consumableCollar.getCollarBeizhu());
		couLyVo.setCollarCkstatus(consumableCollar.getCollarCkstatus());
		couLyVo.setCollarApproveresult(consumableCollar.getCollarApproveresult());
		//领用对象
		couLyVo.setCollarObject(consumableCollar.getCollarObject());
		Tbdepartment tbdepartment = tbdepartmentDAO.findOne(consumableCollar.getCollarObject().longValue());			
		System.out.println("科室名称："+tbdepartment.getDepartmentNo());
		couLyVo.setCollarObjectName(tbdepartment.getDepartmentName());
		//审批人
		couLyVo.setCollarApprovepeople(consumableCollar.getCollarApprovepeople());
		Tbstaff tbstaff1 = tbstaffDAO.findOne(consumableCollar.getCollarApprovepeople().longValue());	
		couLyVo.setCollarApprovepeopleName(tbstaff1.getStaffName());
		System.out.println("审批人姓名"+tbstaff1.getStaffName());
		//领用仓库
		String store = consumableCollar.getCollarStorehouse();
		couLyVo.setCollarStorehouse(store);
		System.out.println(store);
		YzrStorehouse storehouse = storehouseDAO.findOne(store);
		System.out.println("仓库编号："+storehouse.getStoreId());
		couLyVo.setCollarStorehouseName(storehouse.getStoreName());		
		return couLyVo;		
	}
	
	private List<LmbCouYlVo> couPurYlVoToList(List<LmbConsumableCollar> list){
		if(list==null) return null;
		List<LmbCouYlVo> couPurYlVoList = new ArrayList<>();
		for(LmbConsumableCollar consumableCollar:list){
			LmbCouYlVo couPurYlVo = hlPoToVo(consumableCollar);
			couPurYlVoList.add(couPurYlVo);
		}
		return couPurYlVoList;		
	}
	
	public Map<String,Object> queryLmbConsumableCollarByPage(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		//获取当前页的记录
		List<LmbConsumableCollar> list = consumableCollarDAO.findConsumableCollarByPage(pr);
		long total = consumableCollarDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows",couPurYlVoToList(list));
		return map;
	}
	
	//分页查询部门
	public Map<String,Object> queryAllDeptByPage(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		//获取当前页的记录
		List<Tbdepartment> list = tbdepartmentDAO.findTbdepartmentPage(pr);
		long total = list.size();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows",list);
		return map;
	}
	
	//查询所有的部门
	public List<Tbdepartment> queryAllDept(){
		List<Tbdepartment> list = tbdepartmentDAO.queryAllTbdepartment();
		return list;
	}
	
	//根据名称查询部门
	public Map<String,Object> queryAllDeptBydname(int page,int rows,String ksName){
		PageRequest pr = new PageRequest(page - 1, rows);
		//获取当前页的记录
		List<Tbdepartment> list = tbdepartmentDAO.findByTbdepartmentPageDepartmentName(pr, ksName);
		long total = list.size();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows",list);
		return map;
	}
	
	//根据条件模糊查询员工
	public Map<String,Object> queryStaffByIf(int page,int rows,String value){
		PageRequest pr = new PageRequest(page - 1, rows);
		//获取当前页的记录
		List<Tbstaff> list  = tbstaffDAO.findStaffNameByPage(pr, value);
		Map<String,Object> map = new HashMap<>();
		long total = list.size();
		map.put("total", total);
		map.put("rows",list);
		return map;
	}	
		
	 public void hcly(LmbHlVo hlVo){
		String date = oddNumbersService.rq();
		//拼接
		String name = "HL-" + date;					
		String n = consumableCollarDAO.findMaxID(name);
		System.out.println("从数据库中读取的最大采购计划编号："+n);
		String sid =oddNumbersService.getId(name, n);
		LmbConsumableCollar conCollar = hlVo.getCollar();
		conCollar.setCollarId(sid);
		conCollar.setCollarState("待审批");
		conCollar.setCollarCkstatus("未领用");
		consumableCollarDAO.save(conCollar);
		List<LmbConsumableCollarDetail> list = hlVo.getCollarDetail();
		if(list !=null){
			for(LmbConsumableCollarDetail conCollarDet:list){
				String name1 = "HLX-" + date;	
				String n1 = conCollarDetailDAO.findMaxID(name1);
				String sid1 = oddNumbersService.getId(name1, n1);
				conCollarDet.setCdId(sid1);
				conCollarDet.setCdCollarId(sid);
				conCollarDetailDAO.save(conCollarDet);
			}
		}
	}
	
	 //查询当前用户需要审批的领用单
	 public Map<String,Object> queryHclysp(BigDecimal spr,int page,int rows){
		 PageRequest pr = new PageRequest(page - 1,rows);
		 //获取当前页的记录
		 List<LmbConsumableCollar> list = consumableCollarDAO.findByApprovepeople(pr, spr);
		 long total = consumableCollarDAO.countByApprovepeople(spr);
		 Map<String,Object> map = new HashMap<>();
		 map.put("total", total);
		 map.put("rows", couPurYlVoToList(list));
		 return map;
	 }
	 	 
	 //根据领用单编号查询领用详情
	 public List<LmbConsumableCollarDetail> queryHclyxq(String collarId){
		 List<LmbConsumableCollarDetail> list = conCollarDetailDAO.findByCollarId(collarId);
		 return list;
	 }
	 
	 public LmbConsumableCollar queryHclyById(String collarId){
		 return hlPoToVo(consumableCollarDAO.findByCollarId(collarId));
	 }
	 
	 //领用单审批
	 public void hclysp(String collarId,String jg,String bz){
		 LmbConsumableCollar collar = consumableCollarDAO.findOne(collarId);
		 collar.setCollarState("已审批");
		 collar.setCollarApprovedate(new Date());			
		 collar.setCollarApproveresult(jg);
		 collar.setCollarBeizhu(bz);
		 consumableCollarDAO.save(collar);
	 }
	 
	 //查询当前用户已审批的领用单
	 public Map<String,Object> queryHclyYsp(BigDecimal spr,int page,int rows){
		 PageRequest pr = new PageRequest(page - 1,rows);
		 //获取当前页的记录
		 List<LmbConsumableCollar> list = consumableCollarDAO.findYsp(pr, spr);
		 long total = consumableCollarDAO.countfindYsp(spr);
		 Map<String,Object> map = new HashMap<>();
		 map.put("total", total);
		 map.put("rows", couPurYlVoToList(list));
		 return map;
	 }
	 
	 //查询所有的领用单
	 public List<LmbConsumableCollar> queryAllLy(){
		 List<LmbConsumableCollar> list = (List<LmbConsumableCollar>) consumableCollarDAO.findAll();
		 return list;
	 }
	 
	 //查找所有已审批但未领用的领用单
	 public  Map<String,Object> queryAllYspwly(int page,int rows){
		 PageRequest pr = new PageRequest(page - 1,rows);
		 //获取当前页的记录
		 List<LmbConsumableCollar> list = consumableCollarDAO.queryAllYspwly(pr);		
		 long total = consumableCollarDAO.countAllYspwly();
		 System.out.println("total:"+total);
		 Map<String,Object> map = new HashMap<>();
		 map.put("total", total);
		 map.put("rows", couPurYlVoToList(list));
		 return map;
	 }
	 
	 public LmbKcVo hlxPoToVo(LmbConsumableCollarDetail collarDet){
	 	LmbKcVo kcVo= new LmbKcVo();
	 	kcVo.setCdId(collarDet.getCdId());
	 	kcVo.setCdCId(collarDet.getCdCId());
	 	kcVo.setCdCName(collarDet.getCdCName());
	 	kcVo.setCdNorms(collarDet.getCdNorms());
	 	kcVo.setCdNum(collarDet.getCdNum());
	 	kcVo.setCdCollarId(collarDet.getCdCollarId());
	 	kcVo.setCdBatchNum(collarDet.getCdBatchNum());
	 	long num = conStockDAO.findConStockNum(collarDet.getCdCId(), collarDet.getCdBatchNum());
	 	kcVo.setCdKcnum(new BigDecimal(num));
		return kcVo;
	 }
	 
	 private List<LmbKcVo> couPurYlxVoToList(List<LmbConsumableCollarDetail> list){
			if(list==null) return null;
			List<LmbKcVo> couPurYlxVoList = new ArrayList<>();
			for(LmbConsumableCollarDetail collarDetail:list){
				LmbKcVo kcVo = hlxPoToVo(collarDetail);
				couPurYlxVoList.add(kcVo);
			}
			return couPurYlxVoList;		
		}
		
	 
	 public LmbCouYlVo queryLyBylydh(String collarId){
		 LmbConsumableCollar collar = consumableCollarDAO.findOne(collarId);		 
		 return hlPoToVo(collar);
	 }
	 
	 
	 //根据id查找提领单详情（耗材库存数量）
	 public List<LmbKcVo> queryLyxqByLyId(String collarId){
		 List<LmbConsumableCollarDetail> list = conCollarDetailDAO.findByCollarId(collarId);	 
		return couPurYlxVoToList(list);		 
	 }
	 
	//根据id查找提领单详情
	public List<LmbConsumableCollarDetail> queryLyByLyId(String collarId){
		List<LmbConsumableCollarDetail> list = conCollarDetailDAO.findByCollarId(collarId);	 
		return list;		 
	}
	 
	public LmbCouYlVo queryCollar(String id){
		LmbConsumableCollar collar = consumableCollarDAO.findOne(id);
		return hlPoToVo(collar);
	}
	
	
}
